package com.tencent.miniproject.ui;

import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;

import com.tencent.miniproject.R;
import com.tencent.miniproject.base.BaseActivity;
import com.tencent.miniproject.been.QQLogin;
import com.tencent.miniproject.constants.Constant;
import com.tencent.miniproject.presenter.LoginPresenter;
import com.tencent.miniproject.presenter.i.ILoginPresenter;
import com.tencent.miniproject.ui.i.ILoginActivity;
import com.tencent.miniproject.utils.GsonUtil;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

public class LoginActivity extends BaseActivity
        implements ILoginActivity, IUiListener {

    @BindView(R.id.cv_qq_login)
    CircleImageView cvQqLogin;

    //tencent实例
    private Tencent mTencent;

    private ILoginPresenter mPresenter;
    private String mAppid = "";

    @Override
    protected int setLayoutResID() {
        return R.layout.activity_login;
    }

    @Override
    protected void initData() {
        if (getSharedPreferences("appBase", MODE_PRIVATE).getBoolean("loginSuccess", false)){
            startActivity(new Intent(this, MainActivity.class));
            finish();
        }

        mPresenter = new LoginPresenter(this);
        // Tencent类是SDK的主要实现类，开发者可通过Tencent类访问腾讯开放的OpenAPI。
        // 其中APP_ID是分配给第三方应用的appid，类型为String。
        mAppid = Constant.APP_ID;
        mTencent = Tencent.createInstance(mAppid, this.getApplicationContext());
    }

    @Override
    protected void initView() {
        JSONObject jsonObject = null;
        boolean isValid = mTencent.checkSessionValid(mAppid);
        if(!isValid) {
//            showToast("token信息失效");
            return;
        } else {
            jsonObject = mTencent.loadSession(mAppid);
            mTencent.initSessionCache(jsonObject);
        }
    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.cv_qq_login)
    public void onClick() {
//        mTencent.login(this, "ningyuwen", this);
        onClickLogin();
    }

    private void onClickLogin() {
//        if (!mTencent.isSessionValid()) {
//            mTencent.login(this, "all", this);
//            Log.d("SDKQQAgentPref", "FirstLaunch_SDK:" + SystemClock.elapsedRealtime());
//        }

        mTencent.login(this, "all", this);
    }

    @Override
    public void onComplete(Object response) {
        Log.i("ning", "onComplete: " + response.toString());
        QQLogin qqLogin = GsonUtil.gsonToBean(response.toString(), QQLogin.class);
        mPresenter.login(this, qqLogin.getAccess_token(), qqLogin.getOpenid());
    }

    @Override
    public void onError(UiError uiError) {
        Log.i("ning", "onError: " + uiError.errorMessage);
        showToast(uiError.errorMessage);
    }

    @Override
    public void onCancel() {
        showToast("取消");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        mTencent.onActivityResult(requestCode, resultCode, data);
        Log.i("ning", "onActivityResult: " + data.toString());
        Tencent.onActivityResultData(requestCode, resultCode, data, this);
    }

    @Override
    public void jumpToMainActivity() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }
}
